package server.util;

import java.util.HashMap;
import java.util.Map;

/*
封装HashMap,保证返回给Servlet程序的Map无法修改,只能读取
 */
/*
2023/1/28 该类内容全部写完,后续直接使用,无需修改
by cyl
 */
public class ParameterMap<K, V> extends HashMap<K, V> {
    private boolean locked = false;
    public ParameterMap() {
        super();
    }
    public ParameterMap(int initialCapacity) {
        super(initialCapacity);
    }
    public ParameterMap(int initialCapacity, float loadFactor) {
        super(initialCapacity, loadFactor);
    }
    public ParameterMap(Map<? extends K, ? extends V> map) {
        super(map);
    }

    @Override
    public void clear() {
        if (locked)
            throw new IllegalStateException("map已经上锁,不能对内容进行修改");
        super.clear();
    }

    @Override
    public V put(K key, V value) {
        if (locked)
            throw new IllegalStateException("map已经上锁,不能对内容进行修改");
        return super.put(key, value);
    }

    @Override
    public void putAll(Map<? extends K, ? extends V> map) {
        if (locked)
            throw new IllegalStateException("map已经上锁,不能对内容进行修改");
        super.putAll(map);
    }

    @Override
    public V remove(Object key) {
        if (locked)
            throw new IllegalStateException("map已经上锁,不能对内容进行修改");
        return super.remove(key);
    }

    //getter-------------------------------------------------------------------------------------------
    public boolean isLocked() {
        return this.locked;
    }
    //setter-------------------------------------------------------------------------------------------
    public void setLocked(boolean locked) {
        this.locked = locked;
    }
}
